package member.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import member.model.service.MemberService;
import member.to.ActionForward;
import member.to.MemberTO;

import org.json.JSONObject;

public class SearchMemberByMileageRangeController2 implements Controller {
	
	@Override
	public ActionForward handle(HttpServletRequest request,
			HttpServletResponse response) {
		ActionForward af = null;
		String sm = request.getParameter("sMileage");
		String em = request.getParameter("eMileage");
		int sMileage = 0;
		int eMileage = 0;
		try {
			sMileage = Integer.parseInt(sm);
			eMileage = Integer.parseInt(em);
			//Business Logic
			MemberService mms = MemberService.getInstance();
			ArrayList<MemberTO > list = mms.searchMemberByMileageRange(sMileage, eMileage);
			
//			응답 데이터 JSON 문자열로 만들어 ajaxResponse 로 전송
			HashMap map = new HashMap();
			map.put("member_list", list); 
			map.put("title", "마일리지로 검색");
			map.put("keyword", sMileage+" ~ "+eMileage);
			JSONObject jobj = new JSONObject(map);
			request.setAttribute("res_data", jobj.toString());
			af = new ActionForward("/ajaxResponse", false);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			request.setAttribute("error_message","올바르지 않은 검색조건 입니다.");
			af = new ActionForward("/search_form.jsp", false);
			
		} catch (SQLException e) {
			e.printStackTrace();
			request.setAttribute("error_message", e.getMessage());
			af = new ActionForward("/error.jsp", false);
		}
		return af;
	}

}
